package cn.yjr.vector.tools;

import java.util.Stack;

import android.app.Activity;

/**
 * @author vector
 *
 */
public class ActivityManager {
	private static Stack<Activity> activitystack;
	private static ActivityManager activityManager;
	
	
	private ActivityManager(){
		
	}
	public static ActivityManager getActivityManager(){
		if(activityManager==null){
			activityManager = new ActivityManager();
		}
		return activityManager;
	}
	
	public void popActivity(){
		Activity activity = activitystack.lastElement();
		if(activity!=null){
			activity.finish();
			activity = null;
		}
	}
	
	public void popActivity(Activity activity){
		if(activity!=null){
			activity.finish();
			activitystack.remove(activity);
			activity = null;
		}
	}
	
	public Activity getCurrentActivity(){
		Activity activity=activitystack.lastElement();
		return activity;
	}
	
	public void pushActivity(Activity activity){
		if(activitystack==null){
			activitystack=new Stack<Activity>();
		}
		activitystack.add(activity);
	}
	
	public void popAllActivityExceptOne(Class cls){
		while(true){
			Activity activity=getCurrentActivity();
			if(activity==null){
				break;
			}
			if(activity.getClass().equals(cls) ){
				break;
			}
			popActivity(activity);
		}
	}
}
